package website.digitalocean.chapter15;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author: longhc@citycloud.com.cn
 * @since: 2023/2/15 15:00
 * @version: 1.0
 */
@Slf4j
public class MyTopicSubscriber implements Observer {

    private String name;
    private Subject subject;

    public MyTopicSubscriber(String name) {
        this.name = name;
    }

    @Override
    public void update() {
        // 获取消息
        String msg = (String) subject.getUpdate(this);
        if (StrUtil.isBlank(msg)) {
            log.warn("没有新消息; name:{}", name);
            return;
        }
        log.info("收到新消息; name:{}; msg:{}", name, msg);
    }

    @Override
    public void setSubject(Subject subject) {
        this.subject = subject;
    }
}
